﻿using Microsoft.AspNetCore.Mvc;
using WCS_Entity.Dto.RcsDto;
using WCS_Entity.Dto;
using WCS_Service.Interface;
using WCS_Service.Services;
using WCS_Entity.Dto.UpperSysDto;

namespace WCS.Controllers
{
    /// <summary>
    /// 上位服务API管理接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class UpperSysApiController : ControllerBase
    {
        private readonly IUpperSysApiService _upperSysApiService;

        public UpperSysApiController(IUpperSysApiService upperSysApiService)
        {
            _upperSysApiService = upperSysApiService;
        }

        /// <summary>
        /// 查询指定上位服务Api的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> QueryRcsApiAsync(int id)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _upperSysApiService.QueryUppserSysApiAsync(id);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "上位服务Api信息获取失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 查询所有上位服务Api的信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryAllRcsApiAsync()
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _upperSysApiService.QueryAllUppserSysApiAsync();
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "所有上位服务Api信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 查询指定上位服务的所有API
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> QueryRcsApiBaseRcsIdAsync([FromBody] int id)
        {
            var result = new WebApiCallBack();
            var dtoInfoList = await _upperSysApiService.QueryUppserSysApiBaseIdAsync(id);
            if (dtoInfoList is null)
            {
                result.status = false;
                result.msg = "上位服务Api信息获取失败";
                return result;
            }
            result.data = dtoInfoList;
            return result;
        }
        /// <summary>
        /// 删除指定的上位服务Api设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<WebApiCallBack> DeleteAutoEquipmentAsync(int id)
        {
            var result = new WebApiCallBack();
            result.status = await _upperSysApiService.DeleteUppserSysApi(id);
            result.msg = result.status ? "删除成功" : "删除失败";

            return result;
        }
        /// <summary>
        /// 创建上位服务Api
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> CreatRcsApiAsync([FromBody] CreatUpperSysServiceApiDto dto)
        {
            var result = new WebApiCallBack();
            if (string.IsNullOrEmpty(dto.apiName) ||
                dto.upperSysId == 0 ||
                string.IsNullOrEmpty(dto.route))
            {
                result.status = false;
                result.msg = "api名称、上位服务，路由为空";
                return result;
            }
            var dtoInfo = await _upperSysApiService.CreateUppserSysApiAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "创建上位服务Api信息失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
        /// <summary>
        /// 更新上位Api信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> UpdateRcsApiAsync([FromBody] UpdateUpperSysServiceApiDto dto)
        {
            var result = new WebApiCallBack();
            var dtoInfo = await _upperSysApiService.UpdateUppserSysApiAsync(dto);
            if (dtoInfo is null)
            {
                result.status = false;
                result.msg = "更新上位服务Api失败";
                return result;
            }
            result.data = dtoInfo;
            return result;
        }
    }
}
